//根据下标删除目标应用
function delTarApp(idIndex) {
//	console.log("删除目标应用的下标: ", idIndex);
	var url = proName + "/queueAccess/delTarApp";
	var datapost = {
		"idIndex": idIndex,
		"key": key
	};
	$.post(url,datapost,function(){
		//刷新div
		window.location.reload();
	});
	
}


//校验输入框
/*function valid() {
	var checkNameUrl = proName + "/queueAccess/checkName";
    return $("#queueForm").validate({
		rules: {
			topicQueueName: {
                required:true,
                maxlength:32,
                stringCheck:false,
                remote:{
            	    url: checkNameUrl,
              	    type:"post",
                    dataType:"json",
                    async:false,
                    cache:false,
                    data:{
                    	name:function(){
                 		  var name=$("#topicQueueName").val();
                		    return name;
                	 	   },
                	 	   key:function(){
                	 		   return key;
                	 	   }
                 	},
        		       dataFilter:function(data){
        				  return data;
        		       }
             },
                
			},
			topicEmail: {
				required: true,
				maxlength:500,
				email:true,
			}
			
		},
    	messages: {
    		//对应name
    		topicQueueName: {
	            required:"主题队列名称不能为空",
	            maxlength:"最多输入32位",
	            // 校验特殊字符
				stringCheck:jQuery.validator.addMethod("stringCheck", function(value, element) {
					var all= /^[a-zA-Z0-9\u4e00-\u9fa5]+$/;
					return this.optional(element) || all.test(value);       
				}, "主题队列名称不能有特殊字符"),
				remote:"主题队列名重复",
				
    		},
    		topicEmail: {
    			required:"邮箱地址不能为空",
    			maxlength:"最多输入500位",
             	email:"请输入正确邮箱",
    		},
			
    	}
	});
}*/

//监听输入主题队列名
function saveQueueName(obj) {
	var topicQueueName = obj.value;
//	console.log("监听到输入的主题队列名称: ", topicQueueName);
	var url = proName + "/queueAccess/saveQueueName";
	
	//保存主题队列名称到session中
	$.post(url, {'topicQueueName': topicQueueName, 'key':key});
}

//保存消息队列接入
function saveApps() {
//	console.log("----保存消息队列接入----");
	var url = proName + "/queueAccess/queryAppMap";
	var tarAppIds = new Array();

	var topicQueueName = $("#topicQueueName").val();
//	console.log("topicQueueName: ",topicQueueName);
//	console.log("appMap: ", JSON.stringify(appMap));
	
	if(!topicQueueName) {
		window.wxc.xcConfirm("请填写主题队列名称！", window.wxc.xcConfirm.typeEnum.confirm,{
			title: "提示",
			btn: parseInt("0001",2)
		});
		return;
	} 
	else {
		// 校验名称是重复
		var url1 = proName + "/queueAccess/checkName";
		var datapost = {
			"topicQueueName" : topicQueueName,
			"key" : key
		}
		$.post(url1, datapost, function( flag ) {
			if(!flag) {
				window.wxc.xcConfirm("主题队列名称重复！", window.wxc.xcConfirm.typeEnum.confirm,{
					title: "提示",
					btn: parseInt("0001",2)
				});
				return;
			}
			else {
				var topicQueueName = $("#topicQueueName").val();
				if(appMap) {
					var origin = appMap.origin;
					var targetList = appMap.targetList;
//					console.log("目标应用集合: " + JSON.stringify(targetList));
					if(targetList) {
						for (var i=0; i<targetList.length; i++) {
//							console.log("目标应用id: ", app.targetId);
							tarAppIds.push(targetList[i].targetId);
						}
					}
					//源应用没选中
					
					if(!origin) {
						window.wxc.xcConfirm("请先选择源应用！", window.wxc.xcConfirm.typeEnum.confirm,{
							title: "提示",
							btn: parseInt("0001",2)
						});
						return;
					}
					//目标应用没选中
					if(tarAppIds.length == 0)  {
						window.wxc.xcConfirm("请先选择目标应用！", window.wxc.xcConfirm.typeEnum.confirm,{
							title: "提示",
							btn: parseInt("0001",2)
						});
						return;
					}
					//保存源应用和目标应用
					if(origin && tarAppIds.length > 0) {
//						console.log("保存应用")
						var title = "保存";
//						console.log(data);
//						console.log("s:"+editOrNew)
				        var url;
						if(editOrNew){
							url= proName + '/topicQueue/editApps';
						}else{
							url= proName + '/topicQueue/saveApps';
						}
						
						var data = {
							"key": key,
							"topicQueueName" : topicQueueName,
							"status" : status
						};
				        var option = {
							title: title,
							btn: parseInt("0011",2),
							onOk: function(){
								$.post(url, data, function(result) {
//									console.log("保存结果: ", result);
									if(result.state == 200) {
				    					window.wxc.xcConfirm("保存成功！", window.wxc.xcConfirm.typeEnum.confirm,{
				    						title: title,
				    						btn: parseInt("0001",2),
				    						onOk:function(){
				    							window.location = proName + "/topicQueue/page";
				    						}
				    					});
									}else {
				    					window.wxc.xcConfirm(result.message, window.wxc.xcConfirm.typeEnum.confirm,{
				    						title: title,
				    						btn: parseInt("0001",2),
				    						onOk:function(){
				    						}
				    					});
									}
				                });
							}
						}
						window.wxc.xcConfirm("是否保存此消息队列？", "custom", option);
					}
				} else {
					window.wxc.xcConfirm("请先选择源应用和目标应用！", window.wxc.xcConfirm.typeEnum.confirm,{
						title: "提示"
					});
				}
			}
		});
	}
	
	
//	if (valid().form()) {
		
//	}
}

// 新增目标应用选择(先校验之前)
function addTargetApp() {
	//校验上一个目标应用是否写完
	/*var data = {
		"index":targetNum,
		"key":key
	}
	var url = proName + "/queueAccess/checkLastTar";
	$.post(url, data, function(){
		
	});*/
	
	
	
	// 添加虚线组合
	$("#dasheds").append(
    	'<div class="hrsVerDiv">' +
    		'<hr class="hrsVertical"></hr>' +
    	'</div>' +
    	'<hr class="shortHrs"></hr>'
	);
	// 添加应用
	$("#targetAppList").append(
		'<div class="appConf">' +
    		'<div style="width: 100%; ">' +
				'<img src="'+ proName + '/images/queueIcon/selectSystem_gray.png" class="icon" aria-hidden="true"><br>' +	
    		'</div>' +
			'<button type="button" class="btn iconBtn" onclick="checkTargetApp1()">' +
                	'选择应用系统' +
            '</button>' +
		'</div>'
	);
}

// 返回主题队列管理页面
function backQueue() {
//	alert("返回");
	var option = {
		title: "提示",
		btn: parseInt("0011",2),
		onOk:function(){
			window.location = proName + "/topicQueue/page";
		}
	}
	window.wxc.xcConfirm("是否返回主题队列管理页面？", "custom", option);
}

// 选择源应用
function checkOriginApp() {
//	console.log("发布状态: ", status);
	//若已发布，不能修改源应用
	if(status == 1302) {
//		console.log("发布状态下选择源应用");
		window.wxc.xcConfirm("该队列已经发布，无法修改源应用！", window.wxc.xcConfirm.typeEnum.confirm,{
			title: "提示",
			btn: parseInt("0001",2)
		});
		return false;
	}
	window.location = proName + "/app/toseeByParam?param=checkOriginApp&key=" + key;
}

//选择目标应用初始化
function checkTargetApp1() {
	window.location = proName + "/app/toseeByParam?param=checkTargetApp&key=" + key;
}


// 选择更换目标应用
function checkTargetApp(value,index) {
//	alert(index);
	var id = value;
//	console.log("选择的目标应用id: ", id, ", num: " + index);
	//删除Session缓存
	window.location = proName + "/app/toseeByParam?param=checkTargetApp&key=" + key + "&index=" + index;
}



